import request from '@/router/axios';
import Mock from 'mockjs';

//id
// var ids = Mock.mock({"id|1-50":1000});

let _list = [
    {
        content: "你想要的都有",
        enabled: true,  // 是否启用
        id: 1, 
        link: "", //链接
        name: "鞋子",
        position: 1, // 广告位置 1首页  2个人中心  3 购物车
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/lk4mkayq44ikx1sw7kdr.jpg"
    },
    {
        content: "上千种化妆品 等你来选",
        enabled: true,
        id: 2,
        link: "",
        name: "化妆品活动专场",
        position: 1, 
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/zmy3nmbdmas7179blpgv.jpg"
    },
    {
        content: "百货商品全场折扣低价",
        enabled: true,
        id: 3,
        link: "",
        name: "百货活动",
        position: 1,
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/je72dqu6utjgc4zw8i37.jpg"
    },
    {
        content: "儿童专场 应有尽有",
        enabled: true,
        id: 4,
        link: "",
        name: "儿童衣物",
        position: 1,
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/udf5e84pdoc0m927zvl5.jpg"
    },
    {
        content: "你想要的都有",
        enabled: true,  // 是否启用
        id: 5, 
        link: "", //链接
        name: "鞋子",
        position: 1, // 广告位置 1首页  2个人中心  3 购物车
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/lk4mkayq44ikx1sw7kdr.jpg"
    },
    {
        content: "上千种化妆品 等你来选",
        enabled: true,
        id: 6,
        link: "",
        name: "化妆品活动专场",
        position: 1, 
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/zmy3nmbdmas7179blpgv.jpg"
    },
    {
        content: "百货商品全场折扣低价",
        enabled: true,
        id: 7,
        link: "",
        name: "百货活动",
        position: 1,
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/je72dqu6utjgc4zw8i37.jpg"
    },
    {
        content: "儿童专场 应有尽有",
        enabled: true,
        id: 8,
        link: "",
        name: "儿童衣物",
        position: 1,
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/udf5e84pdoc0m927zvl5.jpg"
    },
    {
        content: "你想要的都有",
        enabled: true,  // 是否启用
        id: 9, 
        link: "", //链接
        name: "鞋子",
        position: 1, // 广告位置 1首页  2个人中心  3 购物车
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/lk4mkayq44ikx1sw7kdr.jpg"
    },
    {
        content: "上千种化妆品 等你来选",
        enabled: true,
        id: 10,
        link: "",
        name: "化妆品活动专场",
        position: 1, 
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/zmy3nmbdmas7179blpgv.jpg"
    },
    {
        content: "百货商品全场折扣低价",
        enabled: true,
        id: 11,
        link: "",
        name: "百货活动",
        position: 1,
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/je72dqu6utjgc4zw8i37.jpg"
    },
    {
        content: "儿童专场 应有尽有",
        enabled: true,
        id: 12,
        link: "",
        name: "儿童衣物",
        position: 1,
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/udf5e84pdoc0m927zvl5.jpg"
    },
    {
        content: "你想要的都有",
        enabled: true,  // 是否启用
        id: 13, 
        link: "", //链接
        name: "鞋子",
        position: 1, // 广告位置 1首页  2个人中心  3 购物车
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/lk4mkayq44ikx1sw7kdr.jpg"
    },
    {
        content: "上千种化妆品 等你来选",
        enabled: true,
        id: 14,
        link: "",
        name: "化妆品活动专场",
        position: 1, 
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/zmy3nmbdmas7179blpgv.jpg"
    },
    {
        content: "百货商品全场折扣低价",
        enabled: true,
        id: 15,
        link: "",
        name: "百货活动",
        position: 1,
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/je72dqu6utjgc4zw8i37.jpg"
    },
    {
        content: "儿童专场 应有尽有",
        enabled: true,
        id: 16,
        link: "",
        name: "儿童衣物",
        position: 1,
        url: "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/udf5e84pdoc0m927zvl5.jpg"
    },
]


// 获取广告列表
export const advertList = (data) =>request({
    url:'/advertList',
    method:'get',
    data
})
Mock.mock(/\/advertList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

//添加广告
export const addAdvert = (data) =>request({
    url:'/addAdvert',
    mothed:'post',
    data
});
Mock.mock(/\/addAdvert/,config=>{
    var ids = Mock.mock({"id|20-50":1000});
    let datas = JSON.parse(config.body)
    let {name,content,position,enabled} = datas;
    if(name && content && position && enabled){
        _list.unshift({
            content: content,
            enabled: enabled,
            id: ids.id, 
            link: datas.link?datas.link:'', //链接
            name: name,
            position: position, // 广告位置 1首页  2个人中心  3 购物车
            url:datas.url?datas.url:''
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除数据
export const detAdvert = (data) =>request({
    url:'/detAdvert',
    mothed:'delete',
    data
})
Mock.mock(/\/detAdvert/,config=>{
    let {id} = JSON.parse(config.body);
    if(id){
        for(let index in _list){
            if(_list[index].id==id){
                _list.splice(index,1)
            }
        }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//查询 广告 根据广告标题 name   广告内容content
export const queryAdvert = (data) =>request({
    url:'/queryAdvert',
    mothed:'get',
    data
})
Mock.mock(/\/queryAdvert/,config=>{
    let datas = JSON.parse(config.body);
    let arr = [];
    if(datas.name || datas.content){
        if(datas.name){
            arr = _list.filter((item,index)=>item.name.indexOf(datas.name)!=-1);
        }
        if(datas.content){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.content.indexOf(datas.content)!=-1);
        }
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 修改广告数据
export const editAdvert = (data) =>request({
    url:'/editAdvert',
    mothed:'put',
    data
})
Mock.mock(/\/editAdvert/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.content = datas.content?datas.content:item.content;
                item.enabled= datas.enabled?datas.enabled:item.enabled;
                item.link = datas.link?datas.link:item.link; //链接
                item.name = datas.name?datas.name:item.name;
                item.position=datas.position?datas.position:item.position; // 广告位置 1首页  2个人中心  3 购物车
                item.url=datas.url?datas.url:item.url;
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})